<template>
  <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :visible.sync="visible"
    lock-scroll class="wpwork-dialog wpwork-dialog_center" width="600px">
    <span slot="title" class="custom-dialog-title">
      <span class="title-line"></span>
      <span class="title-text">围栏配置</span>
    </span>
    <el-form ref="dataForm" v-loading="formLoading" :model="dataForm" :rules="dataRule"
      label-width="100px" class="menuForm">
      <wpwork-form-tip-item :label="'配置作业资源'+resourcesName+'作为围栏使用'" prop="mapshow"
        class="leftWhite">
        <el-switch v-model="dataForm.enabledMark" :active-value="1" :inactive-value="0" />
      </wpwork-form-tip-item>
      <wpwork-form-tip-item class="leftWhite2" label="围栏告警类型"
        tip-label="配置终端出入围栏状态改变时，系统在什么时候进行告警和记录" prop="mapshow">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="'1'">出区告警</el-radio>
          <el-radio :label="'2'">入区告警</el-radio>
          <el-radio :label="'3'">出入区均告警</el-radio>
        </el-radio-group>
      </wpwork-form-tip-item>
      <wpwork-form-tip-item label="告警时间" tip-label="配置围栏的生效工作日及每个工作日的时间段" prop="mapshow"
        class="blue_border">
        <el-checkbox-group v-model="dataForm.time" @change="handleCheckedCitiesChange">
          <el-checkbox v-for="(city,index) in cities" :label="index+1"
            :key="city">{{city}}</el-checkbox>
        </el-checkbox-group>
        <!-- <div class="checkbox_div">
          <div v-for="(city,index) in cities" :key="index" class="checkbox_item">
            {{ city }}
          </div>
        </div> -->

      </wpwork-form-tip-item>
      <wpwork-form-tip-item label="告警终端" prop="terminal" class="blue_border">
        <div style="display: flex;justify-content: flex-start;flex-wrap: wrap;">
          <div v-for="(item,key) in dataForm.terminal" :key="key"
            style="background-color: #f4f8ff;padding: 5px 10px;border-radius: 5px;margin: 5px;">
            <!-- <span class="item-name">{{key+1+'、【'+item.label+'】'}}</span> -->
            <span class="item-name">{{item.label}}</span>
            <i class="el-icon-delete" style="color: #F14141;margin-left: 3px;cursor: pointer;"
              @click="deleteSelect2(item.key)"></i>
          </div>
        </div>
        <el-button type="primary" @click="coordinatesVisible2 = true">选择告警终端</el-button>
      </wpwork-form-tip-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('common.cancelButton')}}</el-button>
      <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">
        {{$t('common.confirmButton')}}</el-button>
    </span>
    <el-dialog title="终端选择" :modal="false" :visible.sync="coordinatesVisible2"
      style="margin-left: 32%;margin-top: 11%;" class="ditudialog">
      <el-transfer v-model="selectValue2" :data="list2" :titles="['待选终端','已选终端']"
        filterable></el-transfer>
      <div slot="footer" class="dialog-footer">
        <el-button @click="coordinatesVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="selectSubmit2">确 定</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import iconBox from "@/components/wpwork-iconBox";
import { create, update, info } from "@/api/system/system";
import singleImg from "@/components/Upload/SingleImg";
import clipboard from "@/utils/clipboard";
import request from "@/utils/request";

export default {
  components: { iconBox, singleImg },
  data() {
    return {
      resourcesName: "",
      coordinatesVisible2: false,
      selectValue2: [],
      list2: [
        { key: "1", label: "终端1" },
        { key: "2", label: "终端2" },
        { key: "3", label: "终端3" }
      ],
      checkAll: false,
      cities: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'],
      isIndeterminate: true,
      coordinatesVisible: false,
      visible: false,
      dataForm: {
        id: "",
        terminal: [],
        enabledMark: "",
        type: "",
        time: [],
      },
      dataRule: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        terminal: [{ required: true, message: "类别不能为空", trigger: "blur" }],
      },
      formLoading: false,
      iconBoxVisible: false,
      btnLoading: false,
      selfUrl: ""
    };
  },
  methods: {
    deleteSelect2(id) {
      this.selectValue2 = this.selectValue2.filter(item => item != id);
      this.dataForm.terminal = this.list2.filter(item => this.selectValue2.includes(item.key))
    },
    selectSubmit2() {
      this.dataForm.terminal = this.list2.filter(item => this.selectValue2.includes(item.key))
      console.log("🚀 ~ :136 ~ selectSubmit2 ~ this.dataForm.terminal2:", this.dataForm.terminal2)
      this.coordinatesVisible2 = false;
    },
    handleCheckAllChange(val) {
      this.dataForm.time = val ? this.cities : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
    },
    clearCoordinates() {
      this.dataForm.coordinates = ""
    },
    openMap() {
    },
    openWindow() {
      this.coordinatesVisible = true;
      console.log("🚀 ~ :145 ~ openWindow ~ this.coordinatesVisible:", this.coordinatesVisible)
    },
    closeDataForm() {
      this.dataForm = {
        id: "",
        terminal: [],
        enabledMark: "",
        type: "",
        time: [],
      }
    },
    init(id, resourcesId, resourcesName) {
      this.closeDataForm();
      this.selectValue2 = [];
      this.dataForm.terminal = [];
      console.log("🚀 ~ :107 ~ init ~ id:", id)
      console.log("🚀 ~ :140 ~ dataFormSubmit ~ this.dataForm:", this.dataForm)
      this.dataForm.id = id || "";
      this.dataForm.resourcesId = resourcesId || "";
      this.resourcesName = resourcesName || "";
      this.selfUrl = "";
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.formLoading = true;
          request({
            url: `/api/example/WorkRsfence/getOne`,
            method: "post",
            params: { id: this.dataForm.id }
          }).then((res) => {
            console.log("🚀 ~ :168 ~ init ~ res:", res)
            this.dataForm = res.data;
            this.dataForm.terminal = JSON.parse(this.dataForm.terminal);
            this.dataForm.time = JSON.parse(this.dataForm.time);
            this.selectValue2 = this.dataForm.terminal.map(item => item.key);
            request({
              url: `/api/permission/Users/All`,
              method: "get",
              data: {
                "pageSize": 999,
                "currentPage": 1
              }
            }).then((res) => {
              console.log("🚀 ~ :178 ~ openTerminal ~ res:", res)
              this.list2 = res.data.list.map(item => ({ key: item.id, label: item.realName }));
              console.log("🚀 ~ :185 ~ openTerminal ~ this.selectValue2:", this.selectValue2)
            })
            this.$nextTick(() => {
              this.formLoading = false;
            });
          })
        } else {
          request({
            url: `/api/permission/Users/All`,
            method: "get",
            data: {
              "pageSize": 999,
              "currentPage": 1
            }
          }).then((res) => {
            console.log("🚀 ~ :178 ~ openTerminal ~ res:", res)
            this.list2 = res.data.list.map(item => ({ key: item.id, label: item.realName }));
            console.log("🚀 ~ :185 ~ openTerminal ~ this.selectValue2:", this.selectValue2)
          })
        }
      });
    },
    openIconBox() {
      this.iconBoxVisible = true;
    },
    dataFormSubmit() {
      console.log("🚀 ~ :140 ~ dataFormSubmit ~ this.dataForm:", this.dataForm)
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.formLoading = true;
          const query = {
            ...this.dataForm,
            terminal: JSON.stringify(this.dataForm.terminal),
            time: JSON.stringify(this.dataForm.time)
          };
          if (this.dataForm.id) {
            request({
              url: `/api/example/WorkRsfence/update`,
              method: "post",
              data: query
            }).then(res => {
              console.log("🚀 ~ :210 ~ dataFormSubmit ~ res:", res)
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.btnLoading = false;
                  this.$emit("refreshDataList");
                }
              });
            }).catch(() => {
              this.btnLoading = false;
            });
          } else {
            request({
              url: `/api/example/WorkRsfence/add`,
              method: "post",
              data: query
            }).then(res => {
              console.log("🚀 ~ :210 ~ dataFormSubmit ~ res:", res)
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.btnLoading = false;
                  this.$emit("refreshDataList");
                }
              });
            }).catch(() => {
              this.btnLoading = false;
            });

          }
        }
      });
    },
    choiceIcon(value) {
      this.dataForm.icon = value;
    },
    getSelfUrl(enCode) {
      if (!enCode) return (this.selfUrl = "");
      this.selfUrl = `${window.location.origin}/wpwork_APP_${enCode}`;
    },
    handleCopy(text, event) {
      if (text) clipboard(text, event);
    },
  }
};
</script>
<style lang="scss" scoped>
.img-list {
  display: flex;
  >>> .singleImg-container {
    margin-right: 20px;
    :last-child {
      margin-right: 0;
    }
  }
}
.icon-link {
  color: #409eff;
  margin-left: 10px;
}
.collapse {
  ::v-deep .el-collapse-item__header {
    font-size: 1rem;
    font-weight: 600;
  }
}
.leftWhite {
  ::v-deep .el-form-item__label {
    width: auto !important;
    color: #1a66f2;
    font-weight: 900;
  }
}
.leftWhite2 {
  ::v-deep .el-form-item__label {
    width: 150px !important;
  }
  ::v-deep .el-form-item__content {
    margin-left: 140px !important;
  }
}
.ditudialog {
  margin-left: 31%;
  margin-top: 3%;
  ::v-deep .el-dialog {
    width: 741px;
  }
}

::v-deep .el-dialog__header {
  background-color: #f8f8f8;
  border-bottom: none;
}

.custom-dialog-title {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 0;
  padding: 0;
}

.title-line {
  width: 4px;
  height: 16px;
  background: linear-gradient(to bottom, #3f72e7, #84b0fc);
  margin-right: 10px;
  border-radius: 2px;
}

.title-text {
  font-weight: 900;
  margin: 0;
  padding: 0;
}
.blue_border {
  ::v-deep .el-form-item__content {
    border: 1px solid #d2e4ff;
    border-radius: 5px;
    padding: 0px 0px 5px 5px;
  }
}
.checkbox_div {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  .checkbox_item {
    margin: 5px 10px;
  }
}
</style>